//
//  HomeUploadTableView.swift
//  MT
//
//  Created by M on 2019/12/29.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit

class HomeUploadTableView: UITableView {
    
    /// 模型
    var models: [HomeUploadModel] = []
    
    init(frame: CGRect) {
        super.init(frame: frame, style: .grouped)
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension HomeUploadTableView {
    
    private func setup() {
        delegate = self
        dataSource = self
        separatorStyle = .none
        backgroundColor = .clear
        isScrollEnabled = false
        register(HomeUploadTableViewCell.self, forCellReuseIdentifier: String(describing: HomeUploadTableViewCell.self))
    }
    
}

// MARK: ------------------ 刷新、 删除 ----------------------
extension HomeUploadTableView {
    
    /// 更新上传模型
    public func update(model: HomeUploadModel) {
        let a = Unmanaged.passUnretained(model).toOpaque()
        guard let index = models.firstIndex(where: { (m) -> Bool in
            let a1 = Unmanaged.passUnretained(m).toOpaque()
            return a1 == a
        }) else {
            return
        }
        self.reloadRows(at: [IndexPath(row: index, section: 0)], with: .none)
    }
    
    /// 删除上传模型
    public func remove(model: HomeUploadModel) {
        let a = Unmanaged.passUnretained(model).toOpaque()
        guard let index = models.firstIndex(where: { (m) -> Bool in
            let a1 = Unmanaged.passUnretained(m).toOpaque()
            return a1 == a
        }) else {
            return
        }
        models.remove(at: index)
        self.deleteRows(at: [IndexPath(row: index, section: 0)], with: .none)
    }
    
}

// MARK: ------------------ 所有代理 ----------------------
// MARK: ------------------ UITableViewDelegate ----------------------
extension HomeUploadTableView: UITableViewDelegate {
    
}

// MARK: ------------------ UITableViewDataSource ----------------------
extension HomeUploadTableView: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        models.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: HomeUploadTableViewCell.self), for: indexPath) as? HomeUploadTableViewCell else {
            return tableView.dequeueReusableCell(withIdentifier: String(describing: HomeUploadTableViewCell.self), for: indexPath)
        }
        cell.delegate = self
        cell.model = self.models[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40.0
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
}

// MARK: ------------------ HomeUploadTableViewCellDelegate ----------------------
extension HomeUploadTableView: HomeUploadTableViewCellDelegate {
    
    func uploadTableViewCell(_ cell: HomeUploadTableViewCell, didClickDismissButtonWithModel model: HomeUploadModel) {
        remove(model: model)
    }
    
}
